﻿using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using YawetagLib.Linq;

namespace YawetagLib.Data.L2Sql
{
    internal class DataSource<TEntity> : IDataSource<TEntity> where TEntity : class 
    {
        public DataSource(IQueryable<TEntity> dataSet)
        {
            Table = (Table<TEntity>)dataSet;
        }

        public TEntity Get(int entityID)
        {
            return Table.GetByID(entityID, Extensions.IsDataContextEntityPrimaryKey);
        }

        public TEntity GetOrDefault(int entityID)
        {
            return Table.GetOrDefaultByID(entityID, Extensions.IsDataContextEntityPrimaryKey);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return Table;
        }

        public void Insert(TEntity entity)
        {
            Table.InsertOnSubmit(entity);
        }

        public void Update(TEntity entity)
        {
        }

        public void Delete(TEntity entity)
        {
            Table.DeleteOnSubmit(entity);
        }

        private Table<TEntity> Table { get; set; }
    }
}
